package com.woniuxy.qiantai.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 基于死信队列,配置延迟处理
 */
@Configuration
public class DelayCancelOrderConfig {

    //死信部分
    @Bean
    public FanoutExchange cancleOrderDeadLetterExchange(){
        return new FanoutExchange("cancleOrderDeadLetterExchange");
    }

    @Bean
    public Queue cancleOrderDeadLetterQueue(){
        return new Queue("cancleOrderDeadLetterQueue");
    }

    @Bean
    public Binding cancleOrderDeadLetterBinding(){
        return BindingBuilder.bind(cancleOrderDeadLetterQueue()).to(cancleOrderDeadLetterExchange());
    }


    //normal部分
    @Bean
    public DirectExchange cancleOrderNoramlExchange(){
        return new DirectExchange("cancleOrderNoramlExchange");
    }

    //正常队列不能有消费者
    @Bean
    public Queue cancleOrderNoramlQueue(){
        return QueueBuilder.durable("cancleOrderNoramlQueue")
                .deadLetterExchange(cancleOrderDeadLetterExchange().getName())
                .deadLetterRoutingKey("xxx")  //因为死信部分交换机是Fanout广播类型,所以这里routingKey随便写一个就行
                .ttl(15*60*1000)  //指定ttl
                .build();
    }

    @Bean
    public Binding cancleOrderNoramlBinding(){
        return BindingBuilder.bind(cancleOrderNoramlQueue())
                .to(cancleOrderNoramlExchange())
                .with("delayCancelOrder");
    }


}
